package com.decelo.jpa.redis;

import com.google.gson.Gson;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by Mr.Dxs on 2018/6/4.
 * Redis 工具类
 */
@Component
public class JWTRedisDAO {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations<String,Object> valueOperations;
    //进行List缓存 不好使，需要重写一个list的写入
    //@Resource(name="redisTemplate")
    //private ListOperations<String,Object> listOperations;
    /**
     * 默认过期时长，单位：秒
     */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1;

    private final static Gson gson = new Gson();

    public void set(String key, Object value, long expire){
        // 存到 redis
        valueOperations.set(key,toJson(value));
        // 设置 时效
        if (expire != NOT_EXPIRE){
            redisTemplate.expire(key,expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key,Class<T> clazz, long expire){
        String value =null;
        if (redisTemplate.hasKey(key)){
            value = valueOperations.get(key).toString();
            if (expire != NOT_EXPIRE){
                redisTemplate.expire(key,expire,TimeUnit.SECONDS);
            }
        }
        return value == null ? null : fromJson(value,clazz);
    }


    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value=null;
        //判断是否存在key
        if (redisTemplate.hasKey(key)){
            value = valueOperations.get(key).toString();
            if (value!=null){
                if (expire != NOT_EXPIRE) {
                    redisTemplate.expire(key, expire, TimeUnit.SECONDS);
                }
            }
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String) {
            return String.valueOf(object);
        }
        return gson.toJson(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }

    /**
     *  将map参数对象转换为key
     */
    public String getKeyfromMap(Map<String,Object> keyParams){
        StringBuffer keyBuffer=new StringBuffer();
       /* List<String> keyList=new ArrayList<>();
        for (String keyvalue:keyParams.keySet()) {
            keyList.add(keyvalue);
        }*/
        for(Object value:keyParams.values()){
            keyBuffer.append("#"+value);
        }
        /*for (int i=0;i<keyList.size();i++){
            //根据key角标进行拼接
            if (i==0){
                keyBuffer.append(keyParams.get(keyList.get(i)));
            }else if (i>0){
                keyBuffer.append("#"+keyParams.get(keyList.get(i)));
            }
        }*/
        return keyBuffer.toString();
    }
}
